package cn.qqncn.furniture.controller;

import cn.qqncn.furniture.mapper.CategoryMapper;
import cn.qqncn.furniture.pojo.domain.Category;
import cn.qqncn.furniture.pojo.domain.Furniture;
import cn.qqncn.furniture.pojo.domain.Order;
import cn.qqncn.furniture.pojo.domain.User;
import cn.qqncn.furniture.pojo.vo.CarVo;
import cn.qqncn.furniture.pojo.vo.CheckOutOrderVo;
import cn.qqncn.furniture.service.CarService;
import cn.qqncn.furniture.service.FurnitureService;
import cn.qqncn.furniture.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zkw
 * @ClassName PageController.java
 * @Description TODO
 * @createTime 2020年12月25日 10:51:00
 */
@Controller
public class PageController {
    @Autowired
    FurnitureService furnitureService;

    @Resource
    CategoryMapper categoryMapper;

    @Resource
    CarService carService;

    @Resource
    OrderService orderService;

    /**
     * 跳转修改商品页面
     * @param furnitureId
     * @return 跳转页面
     */
    @RequestMapping("toUpdateFurniture")
    public String toUpdateFurniture(HttpServletRequest request,Integer furnitureId){
//        todo 根据id查询商品 和 全部类别信息，加载到商品修改页面
        Furniture furniture = furnitureService.getFurnitureById(furnitureId);
        List<Category> categories = categoryMapper.selectAll();
        request.setAttribute("furniture",furniture);
        request.setAttribute("categorys",categories);
        return "back/updateFurniture";
    }

    //跳转到shop.jsp
    @RequestMapping("shop")
    public String toShop(){
        return "/front/shop";
    }

    // to  订单提交页面
    @RequestMapping("toCheckOut")
    public String toCheckOut(HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("user");
        List<CarVo> carts = carService.getCar(user.getUserId());
        CheckOutOrderVo checkOutOrderVo = new CheckOutOrderVo();
        checkOutOrderVo.setList(carts);
        double totalPrice = 0;
        for (int i = 0; i < carts.size(); i++) {
            CarVo carVo = carts.get(i);
            totalPrice+=(carVo.getNumber()*carVo.getPrice());
        }
        checkOutOrderVo.setTotalPrice(totalPrice);
        request.setAttribute("checkOutOrderVo",checkOutOrderVo);
        return "/front/checkout";
    }

    // to 我的账户
    @RequestMapping("toMyAccount")
    public String toMyAccount(HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("user");
        List<Order> orders = orderService.selectByUserId(user.getUserId());
        request.setAttribute("orders",orders);
        return "/front/my-account";
    }
}
